package com.huitone.gddw.controller.worktable.paramconfigmanage;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.SvrSpeed;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.ISvrSpeedService;
import com.huitone.gddw.service.dto.SvrSpeedDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短信接入分级控制器
 * @author Weiliyu
 *
 */
@Controller
@RequestMapping(value="svrSpeed")
public class SvrSpeedController extends CommonController {
	
	private final static Logger LOGGER = Logger.getLogger(SvrSpeedController.class);
	
	@Autowired
	private ISvrSpeedService svrSpeedService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<SvrSpeedDTO> query(QueryPage<SvrSpeed> page, HttpServletRequest request){
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询短信接入分级信息";
		Map<String, Object> params = new HashMap<String, Object>();
		String svrNode = request.getParameter("svrNode");
		String serviceCode = request.getParameter("serviceCode");
		String type = request.getParameter("type");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		params.put("svrNode", svrNode);
		params.put("serviceCode", serviceCode);
		params.put("limittype", type);
		params.put("startDate", startDate);
		params.put("endDate", endDate);
		page.setParams(params);
		QueryPage<SvrSpeedDTO> result = null;
		try {
			result = svrSpeedService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询短信接入分级信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}

	/**
	 * 保存
	 * @param entity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(SvrSpeed entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加短信接入分级信息";
		boolean isSuccess = true;
		String msg;
		try {
			
			if(null == entity.getSvrNode() || entity.getSvrNode() ==""){
				isSuccess = false;
				msg = "业务端不为空！";
			} else if(null == entity.getServiceCode() || entity.getServiceCode() ==""){
				isSuccess = false;
				msg = "短信类型不为空！";
			} else if(0 == entity.getFlowspeed()){
				isSuccess = false;
				msg = "流速值/流量值不为空！";
			} else if(null == entity.getLimittype() || entity.getLimittype() ==""){
				isSuccess = false;
				msg = "分级类型不为空！";
			} else if(null == entity.getTimeunit() || entity.getTimeunit() ==""){
				isSuccess = false;
				msg = "单位不为空！";
			} else {
				List<SvrSpeed> list = svrSpeedService.selectBySvr(entity.getSvrNode());
				if (list.size()>0) {
					isSuccess = false;
					msg = "该业务端已存在！";
				}else {
					svrSpeedService.save(entity);
					msg = "保存成功！";
				}
			}
		} catch (Exception e) {
			LOGGER.error("保存短信接入分级信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "保存失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 删除
	 * @param recId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String recId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除短信接入分级信息";
		boolean isSuccess = true;
		String msg;
		try {
			svrSpeedService.delete(recId);
			msg = "删除成功！";
		} catch (Exception e) {
			LOGGER.error("删除短信接入分级信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "删除失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
	/**
	 * 获取要修改的数据
	 * @param recId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public SvrSpeed toUpdate(String recId) {
		try {
			SvrSpeed svrSpeed = svrSpeedService.selectByPrimaryKey(recId);
			return svrSpeed;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return new SvrSpeed();
		}
	}
	
	/**
	 * 修改
	 * @param entity
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(SvrSpeed entity) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改短信接入分级信息";
		boolean isSuccess = true;
		String msg;
		try {
			if(null == entity.getSvrNode() || entity.getSvrNode() ==""){
				isSuccess = false;
				msg = "业务端不为空！";
			} else if(null == entity.getServiceCode() || entity.getServiceCode() ==""){
				isSuccess = false;
				msg = "短信类型不为空！";
			} else if(0 == entity.getFlowspeed()){
				isSuccess = false;
				msg = "流速值/流量值不为空！";
			} else if(null == entity.getLimittype() || entity.getLimittype() ==""){
				isSuccess = false;
				msg = "分级类型不为空！";
			} else if(null == entity.getTimeunit() || entity.getTimeunit() =="" || entity.getTimeunit() == "null"){
				isSuccess = false;
				msg = "单位不为空！";
			} else {
				List<SvrSpeed> list = svrSpeedService.selectBySvr(entity.getSvrNode());
				SvrSpeed svrSpeed = svrSpeedService.selectByPrimaryKey(entity.getRecId());
				if (list.size()>0 && !svrSpeed.getSvrNode().equals(entity.getSvrNode())) {
					isSuccess = false;
					msg = "该业务端已存在！";
					return new Message(isSuccess, msg);
				}
				svrSpeed.setSvrNode(entity.getSvrNode());
				svrSpeed.setServiceCode(entity.getServiceCode());
				svrSpeed.setLimittype(entity.getLimittype());
				svrSpeed.setFlowspeed(entity.getFlowspeed());
				svrSpeed.setTimeunit(entity.getTimeunit());
				svrSpeedService.update(svrSpeed);
				msg = "修改成功！";
			}
		} catch (Exception e) {
			LOGGER.error("修改短信接入分级信息过程中出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
			isSuccess = false;
			msg = "修改失败！";
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return new Message(isSuccess, msg);
	}
	
}
